package com.example.cp.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.cp.config.HiddenProperties;
import com.example.cp.dto.TreeUser;
import com.example.cp.pojo.Ztree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by hemin on 2019/8/16.
 */
@Controller
@RequestMapping("/dept")
public class DeptController {

    private String prefix = "system/dept";

    @Autowired
    RestTemplate restTemplate;

    @Autowired
    HiddenProperties hiddenProperties;

    /**
     * 选择部门树
     */
    @GetMapping("/selectDeptTree")
    public String selectDeptTree(ModelMap mmap)
    {
        return prefix + "/tree";
    }

    /**
     * 加载部门列表树
     */
    @GetMapping("/treeData/{userId}")
    @ResponseBody
    public JSONArray treeData(@PathVariable("userId")String userId)
    {
        String url = hiddenProperties.getHeaderUrl() + "/orgInfo/list?userId={userId}&isAccessFilter=true&isAccessOrgFilter=true";
        JSONArray forObject = restTemplate.getForObject(url, JSONArray.class, userId);
        return forObject;
    }

    /**
     * 选择区域树
     */
    @GetMapping("/selectAreaTree")
    public String selectAreaTree(ModelMap mmap)
    {
        return prefix + "/area";
    }

    /**
     * 加载区域列表树
     */
    @GetMapping("/areaData")
    @ResponseBody
    public JSONArray areaData()
    {
        String url = hiddenProperties.getHeaderUrl() + "/area/list";
        JSONArray forObject = restTemplate.getForObject(url, JSONArray.class);
        return forObject;
    }

    /**
     * 选择人员树
     */
    @GetMapping("/selectUserTree")
    public String selectUserTree(ModelMap mmap)
    {
        return prefix + "/userTree";
    }

    /**
     * 加载人员列表树
     */
    @GetMapping("/userTreeData")
    @ResponseBody
    public List<Ztree> userTreeData(@RequestParam("userId")String userId,
                                    @RequestParam(value = "userName",required = false,defaultValue = "")String userName)
    {
        String url = hiddenProperties.getHeaderUrl() + "/userInfo/list?userId={userId}&isAccessFilter=true&isAccessOrgFilter=true&keyWords={userName}";
        TreeUser[] forObject = restTemplate.getForObject(url, TreeUser[].class, userId,userName);
        List<TreeUser> treeUsers = Arrays.asList(forObject);
        ArrayList<Ztree> ztrees = new ArrayList<>();
        for (TreeUser treeUser : treeUsers) {
            Ztree ztree = new Ztree();
            ztree.setId(treeUser.getId());
            ztree.setName(treeUser.getFullName()+"-"+treeUser.getName()+"("+treeUser.getOrganizationName()+")");
            ztree.setPId("0");
            ztrees.add(ztree);
        }
        if(ztrees == null){
            return ztrees;
        }
        return ztrees.size()>30 ? ztrees.subList(0,30) : ztrees;
    }
}
